﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2012-2-15
/// Purpose: 		生成ORM数据对象的用户控件定义文件
/// ****************************************************************************
/// Modify By		Date			Remark
/// ****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Data;

namespace RB.Developer.Tools
{
    /// <summary>
    /// UserControlORMObject.xaml 的交互逻辑
    /// 生成ORM数据对象的用户控件
    /// </summary>
    public partial class UserControlORMObject : UserControl
    {
        public UserControlORMObject()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtConnStr.Text))
            {
                string connStr = txtConnStr.Text;
                string connectType = ((ComboBoxItem)cmbConnectType.SelectedValue).Content.ToString();

                DbConnection conn = GetConnection();

                if (conn != null)
                {
                    try
                    {
                        conn.Open();
                        var schema = conn.GetSchema("Tables");
                        var schemaView = new DataView(schema);
                        schemaView.Sort = "TABLE_TYPE";

                        cmbView.Items.Clear();
                        cmbTable.Items.Clear();
                        foreach (DataRowView row in schemaView)
                        {
                            cmbView.Items.Add(row["TABLE_NAME"]);
                            if ((string)row["TABLE_TYPE"] != "VIEW")
                                cmbTable.Items.Add(row["TABLE_NAME"]);
                        }
                    }
                    catch
                    {
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }

        private DbConnection GetConnection()
        {
            DbConnection conn = null;
            if (!string.IsNullOrEmpty(txtConnStr.Text))
            {
                string connStr = txtConnStr.Text;
                string connectType = ((ComboBoxItem)cmbConnectType.SelectedValue).Content.ToString();

                if (connectType == "SQL Server")
                {
                    conn = new SqlConnection(connStr);
                }
                else if (connectType == "OLE")
                {
                    conn = new OleDbConnection(connStr);
                }
                else if (connectType == "ODBC")
                {
                    conn = new OdbcConnection(connStr);
                }
            }
            return conn;
        }

        private void cmbView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string value = (string)cmbView.SelectedValue;

            for (int i = 0; i < cmbTable.Items.Count; i++)
            {
                if (value == (string)cmbTable.Items[i])
                {
                    cmbTable.SelectedIndex = i;
                    break;
                }
            }
        }

        private void cmbTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtClassName.Text = (string)cmbTable.SelectedValue;
        }

        private void btnGenerate_Click(object sender, RoutedEventArgs e)
        {
            
            var session = new Dictionary<string, object>();
            session["QueryName"] = cmbView.Text;
            session["TableName"] = cmbTable.Text;
            session["ClassName"] = txtClassName.Text;

            session["QuerySchema"] = GetTableSchema(cmbView.Text);
            session["TableSchema"] = GetTableSchema(cmbTable.Text);

            ORMDataObjectTemplate template = new ORMDataObjectTemplate();
            template.Session = session;
            string result = template.TransformText();

            txtObjectCode.Text = result;
        }

        private DataTable GetTableSchema(string tableName)
        {
            DataTable schema = null;
            DbConnection conn = null;
            DbDataReader reader = null;
            
            try
            {
                conn = GetConnection();
                conn.Open();
                DbCommand cmd = conn.CreateCommand();

                cmd.CommandText = "SELECT * FROM " + tableName;
                reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly);
                schema = reader.GetSchemaTable();
            }
            catch (DbException)
            {
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
            return schema;
        }

        private void btnGenMvcDetail_Click(object sender, RoutedEventArgs e)
        {
            var session = new Dictionary<string, object>();
            session["QueryName"] = cmbView.Text;
            session["TableName"] = cmbTable.Text;
            session["ClassName"] = txtClassName.Text;

            session["QuerySchema"] = GetTableSchema(cmbView.Text);
            session["TableSchema"] = GetTableSchema(cmbTable.Text);

            MVCDetailControlTemplate template = new MVCDetailControlTemplate();
            template.Session = session;
            string result = template.TransformText();

            txtObjectCode.Text = result;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var session = new Dictionary<string, object>();
            session["QueryName"] = cmbView.Text;
            session["TableName"] = cmbTable.Text;
            session["ClassName"] = txtClassName.Text;

            session["QuerySchema"] = GetTableSchema(cmbView.Text);
            session["TableSchema"] = GetTableSchema(cmbTable.Text);

            MVCSearchListTemplate template = new MVCSearchListTemplate();
            template.Session = session;
            string result = template.TransformText();

            txtObjectCode.Text = result;
        }

    }
}
